module.exports = class SenecaService {

    constructor( serverName ){
        this.name( serverName || "cmd" )
        // { cmd : [object] , fn : [function]}
        this.fns = []
        // this.seneca = Seneca()
    }

    name( n ){
        if( n ) this.server = n   //serverName
        return this.server 
    }

    makeCommand( cmd ){
        let tmp = {}
        const kvs = cmd.split(",")
        for( let idx in kvs ){
            const kv = kvs[ idx ].split(":")
            const key = kv[0] ,
                value = kv[1]
            if( key && value ){
                tmp[ key ] = value 
            }
        }
        return tmp 
    }

    /**
     * 
     * @param {*} fn 
     *      function( msg , done )
     */
    add( method , ...args ) {

        let cmd = {}
        let fn = null   //  考虑是否需要默认
        cmd[ this.name() ] = method 
        for( let idx in args ){
            const arg = args[ idx ]
            if( typeof( arg ) == "string"){
                const tmp = this.makeCommand( arg )
                Object.assign( cmd , tmp )
            }else if( typeof( arg ) == "object"){
                Object.assign( cmd , arg )
            }else if( typeof( arg ) == "function"){
                fn = arg 
                continue 
            }
            // this.seneca.add( cmd , function( msg , done ){
            //     done( fn (msg ) )
            // })
        }
        if( fn == null ) throw new Error(method + " 未设置处理函数")
        this.fns.push({
            cmd : cmd , 
            fn : fn 
        })

    }

}